package com.yungam.common.response;

import lombok.Getter;

public class RetDTO<T> {

    public static <T> RetDTO<T> data(T data) {
        return new RetDTO<T>().setCode(Code.success.code).setData(data);
    }

    public static <T> RetDTO<T> success() {
        return new RetDTO<T>().setCode(Code.success.code).setMessage(Code.success.describe);
    }

    public static <T> RetDTO<T> instance() {
        return new RetDTO<T>();
    }

    public static <T> RetDTO<T> error() {
        return new RetDTO<T>().setCode(Code.error.code).setMessage(Code.error.describe);
    }

    public static <T> RetDTO<T> error(String message) {
        return new RetDTO<T>().setCode(Code.error.code).setMessage(message);
    }

    public static <T> RetDTO<T> updateFailure() {
        return new RetDTO<T>().setCode(Code.success_no_update.code).setMessage(Code.success_no_update.describe);
    }

    public static <T> RetDTO<T> updateFailure(String message) {
        return new RetDTO<T>().setCode(Code.success_no_update.code).setMessage(message);
    }

    private RetDTO() {

    }

    @Getter
    public enum Code {
        success(0, "操作成功"),
        success_no_update(1, "更新失败"),
        error(-1, "操作出错");
        private final int code;
        private final String describe;

        Code(int code, String describe) {
            this.code = code;
            this.describe = describe;
        }
    }


    @Getter
    private Integer code;
    @Getter
    private String message;
    @Getter
    private T data;

    public RetDTO<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public RetDTO<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public RetDTO<T> setData(T data) {
        this.data = data;
        return this;
    }
}

